﻿using System;
using System.Net;
using System.Web;
using Snefru.BaseObject;
using Snefru.Interfaces;
using Snefru.Params;
using Snefru.XmlObjects;

namespace Snefru.Managers
{
    class ConfirmationAttackManager : BaseAttack
    {
        public IAttackResult Attack(IAttackParams parameters)
        {
            IAttackResult result = new ConfirmationAttackResult();

            try
            {
                string targetUrl = parameters.TargetUrl.Replace("[SQL]", parameters.QueryPrefix + parameters.Query);

                DateTime StartTime = DateTime.Now;

                string cookieSettings = new HTTPHeaders().GetDetail(HTTPHeaders.XmlPath).HTTPHeadersParameter.Cookie.Replace("Cookie:", "");
                string userAgent = new HTTPHeaders().GetDetail(HTTPHeaders.XmlPath).HTTPHeadersParameter.UserAgent;

                Request = (HttpWebRequest)WebRequest.Create(targetUrl);
                Request.CookieContainer = new CookieContainer();
                Request.CookieContainer.Add(new Uri(targetUrl), new Cookie("snefruCookie", cookieSettings));
                Request.UserAgent = userAgent;
                Response = (HttpWebResponse)Request.GetResponse();
                Response.Close();

                DateTime EndTime = DateTime.Now;
                
                result.StatusCode = Response.StatusCode;
                result.TimeOfResponse = EndTime.Subtract(StartTime);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }
    }
}
